/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.dianlan.processor.business;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import info.peigen.hotpot.component.gateway.dianlan.annotations.DianlanProtocolParse;
import info.peigen.hotpot.component.gateway.dianlan.facade.command.order.DianlanStatusOrder;
import info.peigen.hotpot.component.gateway.dianlan.processor.AbstractDianlanProcessor;
import info.peigen.hotpot.component.gateway.dianlan.protocol.Dianlan;
import info.peigen.hotpot.component.gateway.dianlan.protocol.active.DianlanStubStatusResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.StateMachineEnum;
import org.smartboot.socket.transport.AioSession;

/**
 * <b>(DianlanStatusPushProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/8/23
 */
@Component("dianlanStatusPushProcessor")
@Slf4j
public class DianlanStatusPushProcessor<T> extends AbstractDianlanProcessor implements MessageProcessor<T> {

    @SneakyThrows
    @Override
    public void process(AioSession session, Object obj) {
        Dianlan                   dianlan = (Dianlan) obj;
        DianlanStubStatusResponse status  = DianlanProtocolParse.deserialization(dianlan.getData(), DianlanStubStatusResponse.class);
        if (StrUtil.equals(status.getChargingPortNo(), IGNORE_PORT)) {
            ignorePort(dianlan);
            return;
        }
        log.info("Data:" + JSON.toJSONString(status));
        DianlanStatusOrder order = BeanUtil.copyProperties(status, DianlanStatusOrder.class);
        dianlanServiceFacade.status(order);
    }

    @Override
    public void stateEvent(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
        MessageProcessor.super.stateEvent(session, stateMachineEnum, throwable);
    }

}